{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "710ebb16",
   "metadata": {},
   "source": [
    "### 课堂练习：购物车项目\n",
    "\n",
    "> 要求：\n",
    "1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "2. 让用户输入商品编号进行商品的购买。\n",
    "3. 用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "4. 用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c27aeb9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额：12345678\n",
      "------商品列表信息------\n",
      "商品编号：0， 商品的名称：python课本， 商品的价格：60 元\n",
      "商品编号：1， 商品的名称：华为手机， 商品的价格：8000 元\n",
      "商品编号：2， 商品的名称：小米手环， 商品的价格：1990 元\n",
      "商品编号：3， 商品的名称：蜜雪冰城， 商品的价格：8 元\n",
      "请输入正确的商品编号进行购买(如果退出请输入“q”): 2\n",
      "【小米手环】已经加入您的购物车，您的余额还有 【12343688】 元\n",
      "------商品列表信息------\n",
      "商品编号：0， 商品的名称：python课本， 商品的价格：60 元\n",
      "商品编号：1， 商品的名称：华为手机， 商品的价格：8000 元\n",
      "商品编号：2， 商品的名称：小米手环， 商品的价格：1990 元\n",
      "商品编号：3， 商品的名称：蜜雪冰城， 商品的价格：8 元\n",
      "请输入正确的商品编号进行购买(如果退出请输入“q”): q\n",
      "您的购物车： [['小米手环', 1990]] \n",
      " 您的账户余额：12343688 元\n"
     ]
    }
   ],
   "source": [
    "# 1.运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "# 商品列表数据\n",
    "goods_list = [\n",
    "    # 商品的样式： [商品名称，商品价格],\n",
    "    [\"python课本\",60],\n",
    "    [\"华为手机\",8000],\n",
    "    [\"小米手环\",1990],\n",
    "    [\"蜜雪冰城\",8]\n",
    "]\n",
    "# 写到某一步，发现前面需要内容。可以优化增加\n",
    "user_shopping_cart = [] # 用户列表初始为空，后续用 append 进行增加\n",
    "\n",
    "# 用户输入：input\n",
    "Alipay = int(input(\"请输入您的余额：\"))\n",
    "\n",
    "    # 打印商品信息，打印商品的次数是由商品列表的长度决定的 (len(goods_list))\n",
    "while True: # 将 实现 过程的循环\n",
    "    print(\"------商品列表信息------\")\n",
    "    for i in range(len(goods_list)):\n",
    "        # 给格式化的样式（先给）\n",
    "        # 样式（不懂代码的人可以完成的工作）\n",
    "        #      ：\"商品编号：xx，商品的名称：xx，商品的价格：xx\"\n",
    "        print(\"商品编号：%s， 商品的名称：%s， 商品的价格：%s 元\" %(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "\n",
    "    # 2.让用户输入商品编号进行商品的购买。\n",
    "\n",
    "    # 用户输入商品编号\n",
    "    choice = input(\"请输入正确的商品编号进行购买(如果退出请输入“q”): \")\n",
    "    if choice.isdigit(): # isdigit 判断字符串是否是数值\n",
    "        choice = int(choice) # 如果是数值，把它变成真正的 int\n",
    "        # 判断商品编号是否在 我们的范围内\n",
    "        if choice >= len(goods_list):\n",
    "            print(\"【您输入的商品不存在，请检查再次输入~】\")\n",
    "        else:\n",
    "            # 对比商品编号，找到对应商品：goods_list[choice][1]，\n",
    "            # 1.检查用户余额 VS 商品价格关系\n",
    "            if Alipay > goods_list[choice][1]:\n",
    "                # 买得起 1. 扣钱  2. 把商品加到用户购物列表里\n",
    "                Alipay  = Alipay - goods_list[choice][1]\n",
    "                user_shopping_cart.append(goods_list[choice])\n",
    "                print(\"【%s】已经加入您的购物车，您的余额还有 【%s】 元\" %(goods_list[choice][0],Alipay))\n",
    "            else: # 买不起\n",
    "                print(\"您的余额不足以购买，请更换支付方式\")\n",
    "    elif choice == \"q\":\n",
    "        # 如果输入 q ,则说明用户要退出购买了，我们将打印用户已购买的 商品列表，以及 用户余额\n",
    "        # 利用'\\n'可以实现换行\n",
    "        print(\"您的购物车：\",user_shopping_cart,'\\n',\"您的账户余额：%s 元\"%(Alipay))\n",
    "        break\n",
    "    else:\n",
    "        print(\"请输入正确的【商品编码】！！或者按照输入提示进行输入\")\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fb72545",
   "metadata": {},
   "source": [
    "--------------\n",
    "### 元组（tuple）\n",
    "\n",
    "* [元组]()\n",
    "> 1. 它是list几乎是完全一样的序列数据\n",
    "> 2. 它与list不同的是，**不可变**（因为不可变，所以几乎没有方法）\n",
    "> 3. 元组的形式：（1，2，3，4，5） 对比 元组（） 列表[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "62562dcc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('10001', '10002', '10003', '10001') <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "stu_id = \"10001\",\"10002\",\"10003\",\"10001\"\n",
    "print(stu_id,type(stu_id))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "465c8e8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组第一种方法，查询元素出现的次数\n",
    "stu_id.count(\"10001\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "45500b9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组第二种方法，索引元素\n",
    "stu_id.index(\"10002\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c06614d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=10, tm_mday=25, tm_hour=16, tm_min=21, tm_sec=33, tm_wday=0, tm_yday=298, tm_isdst=0)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "id": "a7524602",
   "metadata": {},
   "source": [
    "-------------\n",
    "### 字典\n",
    "\n",
    "> 1. 创建字典的三种常见的方法\n",
    ">>  1 . 直接按照字典的格式创建 ： {key1:value1 , key2:value2 ,.....}\n",
    ">>  2 . dict()函数来创建字典： dict(key1=value1,key2=value2,...)\n",
    ">>  3 . 字典数据增加的方式： 新建空字典 dict_name = {} , dict_name[key]=value1 ... \n",
    "> 2. 字典的性质 dict是无序的 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4dd0ab45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu_10001': 'Alex', 'stu_10002': 'Jack', 'stu_10003': 'Marry'}\n"
     ]
    }
   ],
   "source": [
    "# 1.直接按照字典的格式创建 ： {key1:value1 , key2:value2 ,.....}\n",
    "stu_info = {\n",
    "    'stu_10001':\"Alex\" , \n",
    "    'stu_10002':\"Jack\" ,\n",
    "    'stu_10003':\"Marry\" \n",
    "}\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "3359ed6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu_10001': 'Alex', 'stu_10002': 'Jack', 'stu_10003': 'Marry'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2. dict()函数来创建字典: dict(key1=value1,key2=value2,...)\n",
    "stu_info_02 = dict(\n",
    "    stu_10001 = \"Alex\",\n",
    "    stu_10002 =\"Jack\" ,\n",
    "    stu_10003 =\"Marry\" \n",
    ")\n",
    "stu_info_02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "e6f56a93",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu_10001': 'Alex', 'stu_10002': 'Jack', 'stu_10003': 'Marry'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. 字典数据增加的方式： 新建空字典 dict_name = {} , dict_name[key]=value1 ... \n",
    "stu_info_03 = {}\n",
    "stu_info_03[\"stu_10001\"] = \"Alex\"\n",
    "stu_info_03[\"stu_10002\"] = \"Jack\"\n",
    "stu_info_03[\"stu_10003\"] = \"Marry\"\n",
    "stu_info_03\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}